<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/6/20
 * Time: 14:43
 */

namespace Biz\service\activity;

use Biz\model\activity\CplRecord as mCplRecord;
use Biz\model\activity\PageModule as mPageModule;
use Doraemon\model\Phone;
use Doraemon\tools\Curl;
use Doraemon\tools\Ip;


class CplRecord
{
    const LOCATION_OF_MUNICIPALITY = ["北京", "上海", "天津", "重庆", "香港", "澳门"];//直辖市区处理

    /*插入 record 数据
     * @param int pageId
     * @param int moduleId
     * @param string name
     * @param string phone
     * @param string area
     * @param int is_save
     * @param int active_uid
     * @return bool result
     * */
    public static function InsertData($page_id, $module_id, $name, $phone, $area, $is_save, $active_uid = 0)
    {
        $info = mCplRecord::getRecordByCondition([
            'page_id' => $page_id,
            'module_id' => $module_id,
            'user_name' => $name,
            'phone' => $phone
        ]);
        $data = [
            'page_id' => $page_id,
            'module_id' => $module_id,
            'user_name' => $name,
            'phone' => $phone,
            'area' => $area,
            'is_save' => $is_save,
            'active_uid' => $active_uid
        ];
        if ($info) {
            $data['id'] = $info['id'];
            $result = mCplRecord::updateRecord($data);
        } else {
            $result = mCplRecord::inserRecord($data);
        }
        return $result ? true : false;
    }

    /*批量获取cpl module中的记录信息
     * @param array moduleIds
     * @param int active_uid
     * @return array hash_record_list
     * */
    public static function getFormatedRecordByModuleIds($moduleIds, $active_uid = 0)
    {
        $hash_record_list = [];
        if ($moduleIds && is_array($moduleIds)) {
            $moduleList = mPageModule::getModuleListByMoudleids($moduleIds);
            if ($moduleList && is_array($moduleList)) {
                $active_user_info = [];
                if ($active_uid) {
                    $phoneInfo = Phone::getByUid($active_uid);
                    if ($phoneInfo && is_array($phoneInfo)) {
                        $active_user_info['phone'] = $phoneInfo['hidden_phone'];
                    }
                    $active_user_info['uid'] = $active_uid;
                }
                //获取地址信息
                $ipArea = self::GetIpArea();
                if ($ipArea) {
                    $active_user_info['area'] = $ipArea;
                }
                //获取地址信息
                foreach ($moduleList as $moduleInfo) {
                    $recordInfo = self::getRecordByModuleId($moduleInfo['id'], $active_user_info);
                    //展示基础信息
                    $basic_field = [];
                    if ($moduleInfo['ext']['basic_field']['name']) {
                        $basic_field['name'] = $recordInfo['name'] ?: '';
                    }
                    if ($moduleInfo['ext']['basic_field']['phone']) {
                        $basic_field['phone'] = $recordInfo['phone'] ?: '';
                        $basic_field['phone_star'] = $recordInfo['phone'] ?: '';
                    }
                    if ($moduleInfo['ext']['basic_field']['area']) {
                        $recordInfo['area'] = trim($recordInfo['area']);
                        $basic_field['area'] = $recordInfo['area'] ?: '北京市 朝阳区';
                    }
                    $hash_record_list[$moduleInfo['id']] =
                        [
                            'basic_field' => $basic_field,
                            'desc' => $moduleInfo['ext']['desc'] ?: '',
                            'submit_desc' => $moduleInfo['ext']['submit_desc'] ?: "提交报名",
                            'joiner_num' => $recordInfo['joiner_num'] * 6 ?: 0
                        ];
                }
            }
        }
        return $hash_record_list;
    }


    /*获取单个cpl 记录的信息
     * @param int moudle_id
     * @param array active_user_info
     * @return array result
     * */
    public static function getRecordByModuleId($module_id, $active_user_info)
    {
        $result = [];
        $recordInfo = mCplRecord::getCplByModuleId($module_id, $active_user_info['uid']);
        if ($recordInfo && is_array($recordInfo)) {
            if ($recordInfo['is_save']) {
                $result['phone'] = $recordInfo['phone'];
                $result['name'] = $recordInfo['user_name'];
                $result['area'] = $recordInfo['area'];
            }
//            else {
//                $result['phone'] = $active_user_info['phone'];
//            }
            //不在获取用户账户uid

        } elseif ($active_user_info) {
            //$result['phone'] = $active_user_info['phone'];
            //不在获取用户账户uid
            $result['phone'] = '';
            $result['name'] = $active_user_info['name'];
            $result['area'] = $active_user_info['area'];
        }
        //展示有多少人参与
        $joiner_count = mCplRecord::getRecordCountByModuleId($module_id);
        $result['joiner_num'] = $joiner_count;
        return $result;
    }


    //根据iP获取地址信息
    public static function GetIpArea()
    {
        $IpArea = '';
        try {
            $clientIp = Ip::getClientIp();
            if (empty($clientIp)) return $IpArea;
            $url = Ip::getIpParserUrl() . $clientIp;
            $res = Curl::Request('GET', $url);
            $data = json_decode($res['data'], true);
            if ($data['Data']['CountryInfo'] && is_array($data['Data']['CountryInfo'])) {
                //直辖市处理
                if (in_array($data['Data']['CountryInfo']['Province'], self::LOCATION_OF_MUNICIPALITY)) {
                    switch ($data['Data']['CountryInfo']['Province']) {
                        case '北京':
                            $IpArea = "北京市 东城区";
                            break;
                        case '天津':
                            $IpArea = "天津市 和平区";
                            break;
                        case '上海':
                            $IpArea = "上海市 黄浦区";
                            break;
                        case '重庆':
                            $IpArea = "重庆市 万州区";
                            break;
                        case '香港':
                            $IpArea = "香港特别行政区 中西区";
                            break;
                        case '澳门':
                            $IpArea = "澳门特别行政区 花地玛堂区";
                            break;
                    }
                } else {
                    $IpArea = $data['Data']['CountryInfo']['Province'] . " " . $data['Data']['CountryInfo']['City'];
                }
            }
            return $IpArea;
        } catch (\Exception $e) {
            return $IpArea;
        }

    }

    //format phone
    public static function formatPhone($phone)
    {
        $phoneStar = '';
        if ($phone) {
            $phone = str_replace(' ', '', $phone);
            $Top3Num = substr($phone, 0, 3);
            $TailNum = substr($phone, 7);
            $phoneStar = $Top3Num . "****" . $TailNum;
        }
        return $phoneStar;
    }

}